using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace THANHTUNGO.Portal.Modules.NEWS.Components
{
    public partial class Item
    {
        #region Private members.

        protected int _ID;
        protected int _CategoryID;
        protected int _FlowID;
        protected string _Title = string.Empty;
        protected string _SubTitle = string.Empty;
        protected string _Content = string.Empty;
        protected string _ImageUrl = string.Empty;
        protected string _Authors = string.Empty;
        protected int _TotalViews;
        protected byte _Rate;
        protected string _Status = string.Empty;
        protected string _Keywords = string.Empty;
        protected DateTime _CreationTime;
        protected DateTime _LastModificationTime;
        protected DateTime _PublishedTime;
        protected string _CreatedBy = string.Empty;

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public int ID
        {
            set { this._ID = value; }
            get { return this._ID; }
        }

        public int CategoryID
        {
            set { this._CategoryID = value; }
            get { return this._CategoryID; }
        }

        public int FlowID
        {
            set { this._FlowID = value; }
            get { return this._FlowID; }
        }

        public string Title
        {
            set { this._Title = value; }
            get { return this._Title; }
        }

        public string SubTitle
        {
            set { this._SubTitle = value; }
            get { return this._SubTitle; }
        }

        public string Content
        {
            set { this._Content = value; }
            get { return this._Content; }
        }

        public string ImageUrl
        {
            set { this._ImageUrl = value; }
            get { return this._ImageUrl; }
        }

        public string Authors
        {
            set { this._Authors = value; }
            get { return this._Authors; }
        }

        public int TotalViews
        {
            set { this._TotalViews = value; }
            get { return this._TotalViews; }
        }

        public byte Rate
        {
            set { this._Rate = value; }
            get { return this._Rate; }
        }

        public string Status
        {
            set { this._Status = value; }
            get { return this._Status; }
        }

        public string Keywords
        {
            set { this._Keywords = value; }
            get { return this._Keywords; }
        }

        public DateTime CreationTime
        {
            set { this._CreationTime = value; }
            get { return this._CreationTime; }
        }

        public DateTime LastModificationTime
        {
            set { this._LastModificationTime = value; }
            get { return this._LastModificationTime; }
        }

        public DateTime PublishedTime
        {
            set { this._PublishedTime = value; }
            get { return this._PublishedTime; }
        }

        public string CreatedBy
        {
            set { this._CreatedBy = value; }
            get { return this._CreatedBy; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static Item Load(int iD)
        {
            string spName = "[dbo].p_NEWS_Item_Load";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, iD);
            Item entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new Item();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CategoryID"))) entity.CategoryID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
                if (!reader.IsDBNull(reader.GetOrdinal("FlowID"))) entity.FlowID = reader.GetInt32(reader.GetOrdinal("FlowID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) entity.Title = reader.GetString(reader.GetOrdinal("Title"));
                if (!reader.IsDBNull(reader.GetOrdinal("SubTitle"))) entity.SubTitle = reader.GetString(reader.GetOrdinal("SubTitle"));
                if (!reader.IsDBNull(reader.GetOrdinal("Content"))) entity.Content = reader.GetString(reader.GetOrdinal("Content"));
                if (!reader.IsDBNull(reader.GetOrdinal("ImageUrl"))) entity.ImageUrl = reader.GetString(reader.GetOrdinal("ImageUrl"));
                if (!reader.IsDBNull(reader.GetOrdinal("Authors"))) entity.Authors = reader.GetString(reader.GetOrdinal("Authors"));
                if (!reader.IsDBNull(reader.GetOrdinal("TotalViews"))) entity.TotalViews = reader.GetInt32(reader.GetOrdinal("TotalViews"));
                if (!reader.IsDBNull(reader.GetOrdinal("Rate"))) entity.Rate = reader.GetByte(reader.GetOrdinal("Rate"));
                if (!reader.IsDBNull(reader.GetOrdinal("Status"))) entity.Status = reader.GetString(reader.GetOrdinal("Status"));
                if (!reader.IsDBNull(reader.GetOrdinal("Keywords"))) entity.Keywords = reader.GetString(reader.GetOrdinal("Keywords"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreationTime"))) entity.CreationTime = reader.GetDateTime(reader.GetOrdinal("CreationTime"));
                if (!reader.IsDBNull(reader.GetOrdinal("LastModificationTime"))) entity.LastModificationTime = reader.GetDateTime(reader.GetOrdinal("LastModificationTime"));
                if (!reader.IsDBNull(reader.GetOrdinal("PublishedTime"))) entity.PublishedTime = reader.GetDateTime(reader.GetOrdinal("PublishedTime"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
            }
            reader.Close();
            dbCommand.Connection.Close();

            return entity;
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectBy_FlowID(int flowID)
        {
            string spName = "[dbo].p_NEWS_Item_SelectBy_FlowID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@FlowID", SqlDbType.Int, flowID);

            return db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------
        public static DataSet SelectBy_CategoryID(int categoryID)
        {
            string spName = "[dbo].p_NEWS_Item_SelectBy_CategoryID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, categoryID);

            return db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------

        public static DataSet SelectAll()
        {
            string spName = "[dbo].p_NEWS_Item_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll()
        {
            string spName = "[dbo].p_NEWS_Item_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_NEWS_Item_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_NEWS_Item_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int Insert(int categoryID, int flowID, string title, string subTitle, string content, string imageUrl, string authors, int totalViews, byte rate, string status, string keywords, DateTime creationTime, DateTime lastModificationTime, DateTime publishedTime, string createdBy)
        {
            Item entity = new Item();
            entity.CategoryID = categoryID;
            entity.FlowID = flowID;
            entity.Title = title;
            entity.SubTitle = subTitle;
            entity.Content = content;
            entity.ImageUrl = imageUrl;
            entity.Authors = authors;
            entity.TotalViews = totalViews;
            entity.Rate = rate;
            entity.Status = status;
            entity.Keywords = keywords;
            entity.CreationTime = creationTime;
            entity.LastModificationTime = lastModificationTime;
            entity.PublishedTime = publishedTime;
            entity.CreatedBy = createdBy;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            string spName = "[dbo].p_NEWS_Item_Insert";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, this._CategoryID);
            db.AddInParameter(dbCommand, "@FlowID", SqlDbType.Int, this._FlowID);
            db.AddInParameter(dbCommand, "@Title", SqlDbType.NVarChar, this._Title);
            db.AddInParameter(dbCommand, "@SubTitle", SqlDbType.NVarChar, this._SubTitle);
            db.AddInParameter(dbCommand, "@Content", SqlDbType.NText, this._Content);
            db.AddInParameter(dbCommand, "@ImageUrl", SqlDbType.NVarChar, this._ImageUrl);
            db.AddInParameter(dbCommand, "@Authors", SqlDbType.NVarChar, this._Authors);
            db.AddInParameter(dbCommand, "@TotalViews", SqlDbType.Int, this._TotalViews);
            db.AddInParameter(dbCommand, "@Rate", SqlDbType.TinyInt, this._Rate);
            db.AddInParameter(dbCommand, "@Status", SqlDbType.NVarChar, this._Status);
            db.AddInParameter(dbCommand, "@Keywords", SqlDbType.NVarChar, this._Keywords);
            db.AddInParameter(dbCommand, "@CreationTime", SqlDbType.DateTime, this._CreationTime);
            db.AddInParameter(dbCommand, "@LastModificationTime", SqlDbType.DateTime, this._LastModificationTime);
            db.AddInParameter(dbCommand, "@PublishedTime", SqlDbType.DateTime, this._PublishedTime);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.NVarChar, this._CreatedBy);

            if (transaction != null)
            {
                db.ExecuteNonQuery(dbCommand, transaction);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
            else
            {
                db.ExecuteNonQuery(dbCommand);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int Update(int iD, int categoryID, int flowID, string title, string subTitle, string content, string imageUrl, string authors, int totalViews, byte rate, string status, string keywords, DateTime creationTime, DateTime lastModificationTime, DateTime publishedTime, string createdBy)
        {
            Item entity = new Item();
            entity.ID = iD;
            entity.CategoryID = categoryID;
            entity.FlowID = flowID;
            entity.Title = title;
            entity.SubTitle = subTitle;
            entity.Content = content;
            entity.ImageUrl = imageUrl;
            entity.Authors = authors;
            entity.TotalViews = totalViews;
            entity.Rate = rate;
            entity.Status = status;
            entity.Keywords = keywords;
            entity.CreationTime = creationTime;
            entity.LastModificationTime = lastModificationTime;
            entity.PublishedTime = publishedTime;
            entity.CreatedBy = createdBy;
            return entity.Update();
        }

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            string spName = "[dbo].p_NEWS_Item_Update";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);
            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, this._CategoryID);
            db.AddInParameter(dbCommand, "@FlowID", SqlDbType.Int, this._FlowID);
            db.AddInParameter(dbCommand, "@Title", SqlDbType.NVarChar, this._Title);
            db.AddInParameter(dbCommand, "@SubTitle", SqlDbType.NVarChar, this._SubTitle);
            db.AddInParameter(dbCommand, "@Content", SqlDbType.NText, this._Content);
            db.AddInParameter(dbCommand, "@ImageUrl", SqlDbType.NVarChar, this._ImageUrl);
            db.AddInParameter(dbCommand, "@Authors", SqlDbType.NVarChar, this._Authors);
            db.AddInParameter(dbCommand, "@TotalViews", SqlDbType.Int, this._TotalViews);
            db.AddInParameter(dbCommand, "@Rate", SqlDbType.TinyInt, this._Rate);
            db.AddInParameter(dbCommand, "@Status", SqlDbType.NVarChar, this._Status);
            db.AddInParameter(dbCommand, "@Keywords", SqlDbType.NVarChar, this._Keywords);
            db.AddInParameter(dbCommand, "@CreationTime", SqlDbType.DateTime, this._CreationTime);
            db.AddInParameter(dbCommand, "@LastModificationTime", SqlDbType.DateTime, this._LastModificationTime);
            db.AddInParameter(dbCommand, "@PublishedTime", SqlDbType.DateTime, this._PublishedTime);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.NVarChar, this._CreatedBy);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int Delete(int iD)
        {
            Item entity = new Item();
            entity.ID = iD;
            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            string spName = "[dbo].p_NEWS_Item_Delete";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        #endregion
    }
}